/*
 * Copyright 2009 Lukasz Wozniak
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *
 * http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package pl.sind.http;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import pl.sind.factory.AbstractFactory;

/**
 * Base class providing ways of instantiating {@link HttpConnectorFactory}. Uses
 * property file to read configured class of factory and optionaly passes this
 * property file to the factory for aditional configuration. Ideally provider of
 * alternative {@link HttpConnectorFactory} provides
 * <code>yajba_http_factory.properties</code> on classpath (in root of his JAR)
 * and his factory is instantiated and returned to the user. Changing JAR
 * results in other factory being used without any change needed in client code.
 * 
 * @author Lukasz Wozniak
 * 
 */
public class BaseHttpConnectorFactory extends
		AbstractFactory<HttpConnectorFactory> {

	protected BaseHttpConnectorFactory() {
		super(HttpConnectorFactory.class);
	}

	public static final String DEFAULT_FACTORY_PROPERTIES = "yajba_http_factory.properties";

	public static final String FACTORY_CLASS_NAME_PROPERTY_NAME = "yajba.factory.classname";

	public static final String DEFAULT_FACTORY_CLASS_NAME = "pl.sind.http.DefaultHttpConnectorFactory";

	private static final Map<? extends Object, ? extends Object> PROPS;

	static {
		HashMap<String,String> props = new HashMap<String, String>();
		props.put(FACTORY_CLASS_NAME_PROPERTY_NAME, DEFAULT_FACTORY_CLASS_NAME);
		PROPS = Collections.unmodifiableMap(props);
	}

	private static AbstractFactory<HttpConnectorFactory> instance;

	/**
	 * Uses default provided factory or the one configured in default property
	 * file.
	 * 
	 * @return instance of {@link HttpConnectorFactory}
	 */
	public static HttpConnectorFactory newInstance() {
		if (instance == null) {
			instance = new BaseHttpConnectorFactory();
		}
		return instance.createFactory();

	}

	/**
	 * Uses provided properties to create factory.
	 * 
	 * @param props
	 *            properties configuring factory. property
	 *            {@link AbstractHttpConnectorFactory#FACTORY_CLASS_NAME_PROPERTY_NAME}
	 *            is mandatory
	 * @return instance of {@link HttpConnectorFactory}
	 */
	public static HttpConnectorFactory newInstance(Properties props) {
		if (instance == null) {
			instance = new BaseHttpConnectorFactory();
		}
		return instance.createFactory(props);
	}

	@Override
	protected Map<? extends Object, ? extends Object> getDefaultProperties() {
		return PROPS;
	}

	@Override
	protected String getDefaultPropertiesFilename() {
		return DEFAULT_FACTORY_PROPERTIES;
	}

	@Override
	protected String getFactoryClassProperty() {
		return FACTORY_CLASS_NAME_PROPERTY_NAME;
	}

}
